package List;

import java.util.ArrayList;
import java.util.Arrays;

public class MyArrayList implements IList {
    public int[] array;
    public int usedSize;
    public static final int default_capacity = 10;//默认常量容量值

    public MyArrayList() {
        this.array = new int[default_capacity];//分配空间，默认值为0
    }

    //扩容
    private void grow () {
        this.array = Arrays.copyOf(this.array, array.length * 2);
    }
    @Override
    //顺序表是否满了
    public boolean isFull() {
        return this.usedSize == this.array.length;
    }

    @Override
    public void add(int data) {
        if (isFull()) {
            grow();
        }
        this.array[this.usedSize] = data;
        this.usedSize++;
    }


    //下标有问题时，抛异常
    private void checkPos(int pos)throws PosIllegal {
        if (pos<0||pos>usedSize){
            throw new PosIllegal("pos位置不合法");
        }
    }
    @Override
    public void add(int pos, int data) {
        try {
            checkPos(pos);
        }catch (PosIllegal e){
            System.out.println("插入位置pos不合法");
            e.printStackTrace();
            return;
        }
        if (isFull()) {
            grow();
        }
        for (int i = this.usedSize-1; i >=pos ; i--) {
            array[i+1]=array[i];
        }
        array[pos]=data;
        this.usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        for (int i=0;i<array.length;i++) {
            if(array[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i=0;i<array.length;i++) {
            if(array[i]==toFind){
                return i;
            }
        }
        return -1;
    }
    private void checkPos2(int pos)throws PosIllegal {
        if (pos<0||pos>=usedSize){
            throw new PosIllegal("pos位置不合法");
        }
    }
    @Override
    public int get(int pos) {
        try {
            checkEmpty();
            checkPos2(pos);
            return array[pos];
        }catch (PosIllegal e){
            System.out.println("寻找位置pos不合法");
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
        return -1;
    }

    public void checkEmpty() {
        if (isEmpty()) {
            throw new EmptyException("顺序表为空！！！！");
        }
    }
    public boolean isEmpty(){
        return usedSize==0;
    }
    @Override
    //更新数组对应位置内容
    public void set(int pos, int value) {
        try {
            checkEmpty();
            checkPos2(pos);
            array[pos]=value;
        }catch (PosIllegal e){
            System.out.println("寻找位置pos不合法");
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try {
            checkEmpty();
            int pos=indexOf(toRemove);
            if (pos==-1){
                return;
            }else {
                for (int i = pos; i <usedSize-1 ; i++) {
                    array[i] =array[i+1];
                }
                usedSize--;
            }
        }catch (EmptyException e){
            e.printStackTrace();
        }

    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        //引用类型防止空间泄露
        /*for (int i = 0; i < usedSize; i++) {
            array[i]=null;
        }*/

        usedSize=0;
    }

    @Override
    // 打印顺序表，注意：该方法并不是顺序表中的方法，为了方便看测试结果给出的
    public void display() {
       /* for (int i:array) {
            System.out.print(i + " ");
        }*/
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(array[i] + " ");
        }
    }
}